namespace SilverSea.Untilities
{
    using System;
    using System.Runtime.InteropServices;

    public static class PCTools
    {
        [DllImport("advapi32.dll", SetLastError=true, ExactSpelling=true)]
        internal static extern bool AdjustTokenPrivileges(IntPtr intptr_0, bool bool_0, ref Struct0 struct0_0, int int_0, IntPtr intptr_1, IntPtr intptr_2);
        public static void DoExitWin(int flg)
        {
            Struct0 struct2;
            IntPtr currentProcess = GetCurrentProcess();
            IntPtr zero = IntPtr.Zero;
            OpenProcessToken(currentProcess, 40, ref zero);
            struct2.int_0 = 1;
            struct2.long_0 = 0L;
            struct2.int_1 = 2;
            LookupPrivilegeValue(null, "SeShutdownPrivilege", ref struct2.long_0);
            AdjustTokenPrivileges(zero, false, ref struct2, 0, IntPtr.Zero, IntPtr.Zero);
            ExitWindowsEx(flg, 0);
        }

        [DllImport("user32.dll", SetLastError=true, ExactSpelling=true)]
        internal static extern bool ExitWindowsEx(int int_0, int int_1);
        [DllImport("kernel32.dll", ExactSpelling=true)]
        internal static extern IntPtr GetCurrentProcess();
        public static void Logoff()
        {
            DoExitWin(0);
        }

        [DllImport("advapi32.dll", SetLastError=true)]
        internal static extern bool LookupPrivilegeValue(string string_0, string string_1, ref long long_0);
        [DllImport("advapi32.dll", SetLastError=true, ExactSpelling=true)]
        internal static extern bool OpenProcessToken(IntPtr intptr_0, int int_0, ref IntPtr intptr_1);
        public static void Restart()
        {
            DoExitWin(2);
        }

        public static void Shutdown()
        {
            DoExitWin(1);
        }

        [StructLayout(LayoutKind.Sequential, Pack=1)]
        internal struct Struct0
        {
            public int int_0;
            public long long_0;
            public int int_1;
        }
    }
}

